﻿using DoNet.Core;
using DoNet.System.Dtos;
using DoNet.System.IRepositories;
using DoNet.System.IServices;
using DoNet.System.Models;

namespace DoNet.System.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class OrganizeService : BaseService<Organize, OrganizeOutputDto>, IOrganizeService
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IOrganizeRepository repository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_baseRepository"></param>
        /// <param name="_repository"></param>
        public OrganizeService(IRepository<Organize> _baseRepository, IOrganizeRepository _repository)
        {
            baseRepository = _baseRepository;
            repository = _repository;
        }

        #region 同步方法
        /// <summary>
        /// 同步软删除状态变更时 deleteMark为true时子部门需要同步更新 deleteMark为false时父部门需要同步更新
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <param name="deleteUserId">软删除操作人Id</param>
        /// <param name="deleteTime">软删除操作时间</param>
        /// <param name="id">组织机构Id</param>
        /// <returns></returns>
        public bool SetDeleteMark(bool deleteMark, long deleteUserId, DateTime deleteTime, long id)
        {
            IEnumerable<long>? ids = null;
            if (deleteMark == true)
            {
                ids = repository.GetChildListById(id).Where(x => x.DeleteMark == !deleteMark).Select(x => x.Id);
            }
            else
            {
                ids = repository.GetParentListById(id).Where(x => x.DeleteMark == !deleteMark).Select(x => x.Id);
            }
            if (ids != null)
            {
                return repository.SetDeleteMark(deleteMark, deleteUserId, deleteTime, ids);
            }
            return false;
        }
        #endregion 同步方法

        #region 异步方法
        /// <summary>
        /// 异步软删除状态变更时 deleteMark为true时子部门需要同步更新 deleteMark为false时父部门需要同步更新
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <param name="deleteUserId">软删除操作人Id</param>
        /// <param name="deleteTime">软删除操作时间</param>
        /// <param name="id">组织机构Id</param>
        /// <returns></returns>
        public async Task<bool> SetDeleteMarkAsync(bool deleteMark, long deleteUserId, DateTime deleteTime, long id)
        {
            IEnumerable<long>? ids = null;
            if (deleteMark == true)
            {
                ids = (await repository.GetChildListByIdAsync(id)).Where(x => x.DeleteMark == deleteMark).Select(x => x.Id);
            }
            else
            {
                ids = (await repository.GetParentListByIdAsync(id)).Where(x => x.DeleteMark == deleteMark).Select(x => x.Id);
            }
            if (ids != null)
            {
                return await repository.SetDeleteMarkAsync(deleteMark, deleteUserId, deleteTime, ids);
            }
            return false;
        }
        #endregion 异步方法
    }
}
